package com.example.b_308.myapplication.service;

import com.example.b_308.myapplication.comment.OfficeApplication;
import com.example.b_308.myapplication.comment.net.INetCallBack;
import com.example.b_308.myapplication.comment.utils.SharedPreferencesUtils;
import com.example.b_308.myapplication.model.OfficeResult;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.model.base.BaseSetting;
import com.example.b_308.myapplication.model.base.OfficeContent;
import com.example.b_308.myapplication.view.user.login.IUserView;

/**
 * Created by B-308 on 2017/11/28.
 */
public class UserService extends BaseService {
    static Staff userDao ;
    IUserView iUserView;

    public UserService(IUserView iUserView) {
        this.iUserView = iUserView;
        userDao = SharedPreferencesUtils.getObj(OfficeApplication.
                getOfficeApplication(), Staff.USER_TAG, Staff.class);
        if(null==userDao)
            userDao=new Staff();
    }

    public void login(Staff staff) {
        userDao.postData("/user/login", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if (OfficeContent.SUCCESS_CODE == officeResult.code) {
                    userDao = new Staff(officeResult.data);
                    SharedPreferencesUtils.putObj(OfficeApplication.
                            getOfficeApplication(), Staff.USER_TAG, userDao);
                    iUserView.loginSuccess(userDao);
                }else{
                    iUserView.fail(officeResult.message);
                }
            }
        });
    }

    public void register(Staff staff) {
        userDao.postData("/user/register", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if (OfficeContent.SUCCESS_CODE == officeResult.code) {
                    userDao = new Staff(officeResult.data);
                    iUserView.registerSuccess();
                }else{
                    iUserView.fail(officeResult.message);
                }
            }
        });
    }

    public void getCode(Staff staff) {
        userDao.postData("/user/getCode", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if (OfficeContent.SUCCESS_CODE == officeResult.code) {
                    userDao = new Staff(officeResult.data);
                    iUserView.getCodeSuccess();
                }else{
                    iUserView.fail(officeResult.message);
                }
            }
        });
    }

    public void loginDefault(){
        userDao.postData("/user/loginDefault", userDao, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if (OfficeContent.SUCCESS_CODE == officeResult.code) {
                    userDao = new Staff(officeResult.data);

                }else{
                    iUserView.fail(officeResult.message);
                }
            }
        });
    }
}
